<template>
  <div class="app-container">
    <zhezhao :is-show="isShow" />
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="100px">
        <el-row>
          <el-col :span="7">
            <el-form-item label="服务名称">
              <el-input v-model="form.service_name" placeholder="请输入服务名称" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="12" class="alignRight">
            <el-button type="primary" @click="searchList()">查询</el-button>
            <el-button
              v-if="!hasPermission('adminRole')"
              class="leadingOut"
              type="primary"
              @click="toRoutes('serverAdd')"
            >添加服务</el-button>
          </el-col>
        </el-row>
      </el-form>
      <!-- 主页列表 -->
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane
          :label="item.label +(serverTab[index] ? serverTab[index] : '') "
          :name="item.name"
          v-for="(item,index) in tabList"
          :key="index"
        >
          <div v-if="item.name==activeName" v-loading="tableLoading" class="listDiv">
            <!-- table列表 -->
            <el-table :data="tableData" style="width: 100%">
              <el-table-column fixed type="index" label="序号" width="50px" align="center" />
              <el-table-column label="服务照片" align="center">
                <template v-if="scope.row.service_image_ids[0]" slot-scope="scope">
                  <img :src="imgUrl + scope.row.service_image_ids[0].url" />
                </template>
              </el-table-column>
              <el-table-column prop="service_name" label="服务名称" align="center" />
              <el-table-column prop="category_name" label="服务分类" align="center" />
              <el-table-column prop="origin_price" label="原价" align="center" />
              <el-table-column prop="price" label="现价" align="center" />
              <el-table-column prop="division_rate" label="分佣比例" align="center" />
              <el-table-column prop="created_at" label="创建时间" width="100px" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.created_at | dateFilter }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="sort" label="排序" align="center" />
              <el-table-column label="审核状态" align="center">
                <template slot-scope="scope">
                  <span
                    :class="scope.row.check_status==1?'col_ora':scope.row.check_status==2?'col_red':'col_gre'"
                  >{{ scope.row.check_status | examineType }}</span>
                  <i
                    v-if="scope.row.check_status==2"
                    class="el-icon-warning"
                    @click="rejectionBtn(scope.row.id,scope.row.check_msg)"
                  />
                </template>
              </el-table-column>
              <el-table-column label="上架状态" align="center">
                <template
                  v-if="scope.row.check_status==3"
                  slot-scope="scope"
                >{{ scope.row.status | statusType }}</template>
              </el-table-column>
              <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                  <!-- 查看、编辑、修改按钮 -->
                  <el-button type="text" size="mini" @click="toRoutes('serverInfo',scope.row.id)">查看</el-button>
                  <el-button
                    v-if="scope.row.check_status==3&&scope.row.status==0"
                    type="text"
                    size="mini"
                    @click="toRoutes('serverEdit',scope.row.id)"
                  >编辑</el-button>
                  <el-button
                    v-if="scope.row.check_status==2"
                    type="text"
                    size="mini"
                    @click="toRoutes('serverEdit',scope.row.id)"
                  >修改</el-button>
                  <!-- 上架、下架操作按钮 -->
                  <el-button
                    v-if="scope.row.status==0&&scope.row.check_status==3"
                    size="mini"
                    type="text"
                    @click="UpperStatus(scope.row.id,scope.row.service_name,1)"
                  >立即上架</el-button>
                  <el-button
                    v-if="scope.row.status==1&&scope.row.check_status==3"
                    size="mini"
                    type="text"
                    @click="UpperStatus(scope.row.id,scope.row.service_name,0)"
                  >立即下架</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="pagesCon">
              <div>
                <el-pagination
                  :current-page="form.page"
                  :page-sizes="page_sizes"
                  :page-size="form.limit"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="page_totals"
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                />
              </div>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>
<script>
import serverList from "@/api/serverList";
import zhezhao from "@/components/CreateMask/index";

export default {
  name: "serverList",
  components: { zhezhao },
  filters: {
    // 审核状态过滤    审核状态下拉列表 1-审核中 2-驳回 3-审核通过
    examineType(e) {
      switch (e) {
        case 3:
          return "审核通过";
          break;
        case 1:
          return "审核中";
          break;
        case 2:
          return "驳回";
          break;
        default:
          return "暂无";
          break;
      }
    },
    // 上架状态过滤 上架状态，0：下架，1：上架
    statusType(e) {
      switch (e) {
        case 0:
          return "已下架";
          break;
        case 1:
          return "已上架";
          break;
        default:
          return "暂无";
          break;
      }
    },
    // 时间戳转化
    dateFilter(e) {
      if (e != 0) {
        return new Date(Number(e) * 1000).format("yyyy-MM-dd hh:mm:ss");
      } else {
        return "";
      }
    }
  },
  data() {
    return {
      // loading层
      tableLoading: false,
      // tabs数据 默认展示全部
      activeName: "1",
      tabList: [
        { label: "全部", name: "1" },
        { label: "上架服务", name: "2" },
        { label: "下架服务", name: "3" },
        { label: "审核中", name: "4" },
        { label: "驳回服务", name: "5" }
      ],
      imgUrl: "http://mmkapp.oss-cn-beijing.aliyuncs.com/",
      // 提交form
      form: {
        service_name: "",
        type: "1",
        page: 1,
        pageSize: 10
      },
      serverTab: [],
      //遮罩层显示隐藏
      isShow: false,
      // 主页列表显示
      tableData: [],
      // 弹窗title信息
      dialogTitle: "",
      // 提示信息
      groundingInfo: null,
      // 驳回弹窗数据信息
      checkForm: {
        id: ""
      },
      // 定义空对象取值
      UpperInfo: {},
      //定义审核状态空值
      checkStatus: "",
      // 驳回弹成功提示语
      msg: "",
      // 分页
      page_sizes: [10, 30, 50, 100],
      page_totals: 60
    };
  },
  inject: ["reload"],
  //监听tab切换的状态值
  watch: {
    activeName(val, oldVal) {
      // 普通的watch监听
      // console.log("activeName: " + val);
      this.form.type = val;
      // console.log(this.form.type);
      this.getList(this.form);
    },
    tableData(val) {
      // console.log(val)
      // this.tableData = val;
    }
  },

  created() {
    this.getSource();
  },
  mounted() {
    this.getList(this.form);
    const data = this.$route.query.userId;
  },
  methods: {
    // 按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    // 获取列表
    getList(e) {
      this.tableLoading = true;
      serverList
        .getList(e)
        .then(res => {
          // console.log(res);
          if (res && res.data.code == 200) {
            this.tableData = res.data.data.data;
            if (this.activeName == "2") {
              this.serverTab[1] = res.data.data.total;
            } else if (this.activeName == "3") {
              this.serverTab[2] = res.data.data.total;
            } else if (this.activeName == "4") {
              this.serverTab[3] = res.data.data.total;
            } else if (this.activeName == "5") {
              this.serverTab[4] = res.data.data.total;
            }

            this.page_totals = res.data.data.total;
            this.tableLoading = false;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {
          this.errorStatus(err.msg);
          this.tableLoading = false;
        });
    },

    // 搜索
    searchList() {
      this.form.page = 1;
      this.getList(this.form);
    },
    // 上下架状态修改
    UpperStatus(id, name, status) {
      this.UpperInfo = { id, status };
      if (status == 1) {
        this.$confirm(
          "您的服务已经提交审核，审核通过后，系统自动上架",
          "上架提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        )
          .then(() => {
            serverList
              .UpperStatus(this.UpperInfo)
              .then(res => {
                if (res.data.code == 200) {
                  this.reload();
                  this.$message({
                    type: "success",
                    message: res.data.msg
                  });
                }
              })
              .catch(err => {
                this.errorStatus(err.msg);
                this.tableLoading = false;
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消"
            });
          });
      } else {
        this.$confirm("您正在下架" + name + "服务", "下架提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            serverList.UpperStatus(this.UpperInfo).then(res => {
              //全部核销
              if (res.data.code == 200) {
                this.reload();
                this.$message({
                  type: "success",
                  message: res.data.msg
                });
                //有未核销的体验券
              } else if (res.data.code == 500) {
                this.reload();
                this.$message({
                  type: "warning",
                  message: res.data.msg
                });
              }
            });
            // .catch(err => {
            //   this.errorStatus(err.msg);
            this.tableLoading = false;
            // });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消"
            });
          });
      }
    },
    // 驳回原因！按钮事件
    rejectionBtn(id, reason) {
      // console.log(id);
      this.$confirm(reason + ",请修改后再次提交~", "驳回理由", {
        confirmButtonText: "重新修改",
        type: "warning",
        center: true
      })
        .then(() => {
          this.toRoutes("serverEdit", id);
          // this.$message({
          // type: "success"
          // message: "成功!"
          // });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status
      });
    },
    // 成功状态弹窗
    succStatus(txt, status) {
      this.eleMsg(txt, "success");
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    },
    // 警告状态弹窗
    warningStatus(txt) {
      this.eleMsg(txt, "warning");
    },
    // 路由跳转
    toRoutes(e, userId) {
      const route = { name: e, query: { userId: userId } };
      this.$router.push(route);
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },
    // 取消弹窗
    cancel() {
      this.dialogFormVisible = false;
      this.$message("已取消");
    },
    // confirm框
    eleConfirm(e) {
      this.$confirm(e.desc, e.title, {
        confirmButtonText: e.confirmBtnTxt,
        cancelButtonText: e.cancelBtnTxt,
        type: "warning"
      })
        .then(() => {
          e.fn();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: e.catchInfo
          });
        });
    },
    // 获取审核状态
    getSource() {
      let source = sessionStorage.getItem("source");
      let status = sessionStorage.getItem("check_status");
      if (status == 3) {
        this.isShow = false;
      } else {
        this.isShow = true;
      }
    }
  }
};
</script>
<style scoped>
.app-container {
  padding: 20px !important;
}
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
/* 表单样式修改 */
/* .el-range-editor--medium.el-input__inner {
  width: 236px;
} */
.switchBtn {
  color: #ff4949;
}
/* 弹窗span文本样式 */
/* .el-form-item__content {
  margin: 0px !important;
  text-align: center;
} */

img {
  display: block;
  width: 80px;
  height: 80px;
}
/* 审核状态文字颜色样式 */
.col_ora {
  color: orange;
}
.col_gre {
  color: green;
}
.col_red {
  color: red;
}
i {
  cursor: pointer;
}
</style>
